#!/usr/bin/env python
# -*- coding:utf-8 -*-

import urllib2
import json
import jsonpath

url = "http://www.lagou.com/lbs/getAllCitySearchLabels.json"
headers = {"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"}
request = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(request)
content = response.read()


# json 转 python
# string 转 unicode
unicodestr = json.loads(content)
# python形式的列表，还是unicode
city_list = jsonpath.jsonpath(unicodestr, '$..name')

# python 转 json
# list 转 array
# 传False就是返回的unicode字符串
array = json.dumps(city_list, ensure_ascii=False)
with open('lagoucity.json', 'w') as f:
    f.write(array.encode('utf-8'))
# print(array)
